﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using desktop.db;
using desktop.Entity;

namespace desktop.Login
{
    class LoginHandler
    {
        private Database _db;

        public LoginHandler(Database db)
        {
            _db = db;
            LogiraniUposlenik = null;
        }


        #region Properties

        public Uposlenik LogiraniUposlenik
        {
            get;
            private set;
        }

        #endregion


        public bool Login(string username, string pw, string tipUposlenika)
        {
            string query;
            query = string.Format("SELECT * FROM zaposleni WHERE username = '{0}' AND password = '{1}' AND tip = '{2}'", username, pw, tipUposlenika);

            if (!_db.ExecuteQuery(query))
            {
                return false;
            }

            TipUposlenika tip = TipUposlenika.Prodavac;
            switch (_db.getString("tip"))
            {
                case "Menadzer":
                    tip = TipUposlenika.Menadzer;
                    break;
                case "Serviser":
                    tip = TipUposlenika.Serviser;
                    break;
                case "Dostavljac":
                    tip = TipUposlenika.Dostavljac;
                    break;
                default:
                    tip = TipUposlenika.Prodavac;
                    break;
            }

            LogiraniUposlenik = new Uposlenik( _db.getInt("id"),
                                               _db.getString("ime"),
                                               _db.getString("prezime"),
                                               _db.getString("jmbg"),
                                               _db.getString("brojTelefona"),
                                               _db.getDate("datumZaposlenja"),
                                               _db.getDate("datumRodjenja"),
                                               _db.getDecimal("plata"),
                                               _db.getDecimal("bonus"),
                                               _db.getString("username"),
                                               tip );

            return true;
        }






    }
}
